﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.IO;

namespace mamnontuoitho7.backend
{
    public partial class AlbumEdit : System.Web.UI.Page
    {
        private Album _album = new Album();
        private Product _product = new Product();
        private Member _mem = new Member();
        protected DataTable _dt;

        protected string _productID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.QueryString["ProductID"] != null)
                    {
                        _productID = Request.QueryString["ProductID"].ToString();
                        hid_AlbumID.Value = _productID;
                    }
                    if (Request.Form["hidProductID"] != null)
                    {
                        _productID = Request.Form["hidProductID"].ToString();
                        hid_AlbumID.Value = _productID;
                    }
                    if (_productID == "")
                    {
                        _productID = hid_AlbumID.Value;
                    }

                    _dt = _album.GetAlbumByIDAdmin(_productID);
                    if (!IsPostBack)
                    {
                        txtTitleName.Text = _dt.Rows[0]["Title"].ToString();
                        txtDescription.Text = _dt.Rows[0]["Introduce"].ToString();
                        imgAlbum.ImageUrl = "../images/album/album-"+_productID +"/"+_dt.Rows[0]["AlbumPhoto"].ToString();
                        hid_TitleName.Value = _dt.Rows[0]["Title"].ToString();
                        hid_Description.Value = _dt.Rows[0]["Introduce"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnAlbumEdit_Click(object sender, EventArgs e)
        {
            try
            {
                _productID = hid_AlbumID.Value;
                if (!string.IsNullOrEmpty(fulPhoto.FileName))
                {
                    bool checkupdate = _album.AlbumUpdate(_productID, txtTitleName.Text.Trim(), txtDescription.Text.Trim(), Session["username"].ToString());

                    string dir = Request.PhysicalApplicationPath + "images/album/album-" + _productID + "/";

                    string localfile = dir + _dt.Rows[0]["AlbumPhoto"].ToString();
                    if (File.Exists(localfile))
                    {
                        File.Delete(localfile);
                    }
                    string[] splitFileName = (fulPhoto.FileName.Split(new char[] { '.' }));
                    string fileExtension = splitFileName[splitFileName.Length - 1].Trim().ToLower();
                    if (fileExtension == "jpg" || fileExtension == "gif" || fileExtension == "jpeg" || fileExtension == "png" || fileExtension == "bmp" || fileExtension == "tiff")
                    {
                        string code = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                        string name = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                        ThumbnailClass thumb = new ThumbnailClass();
                        String filename = thumb.UploadImage(fulPhoto, dir, code, name, _productID);
                        _album.UpdateAlbumThumbmail(filename, _productID);
                        imgAlbum.ImageUrl = "../images/album/album-" + _productID + "/" + filename ;
                    }
                    else
                    {
                        String script = "<script language=JavaScript>alert('Chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff')</script>";
                        Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                    }
                }
                else
                {
                    bool checkupdate = _album.AlbumUpdate(_productID, txtTitleName.Text.Trim(), txtDescription.Text.Trim(), Session["username"].ToString());
                }
                String script1 = "<script language=JavaScript>alert('Cập nhật thành công'); window.location = '../backend/AlbumEdit.aspx?ProductID="+_productID+"';</script>";
                Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

        protected void btnAlbumRefresh_Click(object sender, EventArgs e)
        {
            txtTitleName.Text = hid_TitleName.Value;
            txtDescription.Text = hid_Description.Value;
        }
    }
}